{***************************************************************
    SpyGestion  Copyright (C) 2008 Christophe KOALI <nature2gas@gmail.com>
    
    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.
    
    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.
    
    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
    
****************************************************************}
unit modListeAttaqueVaisseauxInfos;

interface


uses modFlotteVaisseauxChoix, modBaseDeDonnesRapport,
     modBaseType,
     modClasses,
     modPlanete,
     sFonctionsChaines,
     modRapport,
     Controls, ExtCtrls,
     Math,


     Classes, SysUtils;


const
    CNT_LA_VAISSEAUX_TYPE_MAX_INDICE = 6;


type
    TLAVaisseauxInfosOne = record
       gid     : integer;
       quantite: integer;
    end;


    TLAVaisseauxInfos = array[0..CNT_LA_VAISSEAUX_TYPE_MAX_INDICE] of TLAVaisseauxInfosOne;

    PLAVaisseauxInfos = ^TLAVaisseauxInfos;


    procedure mlaviInitListeT(var l: TLAVaisseauxInfos);
    procedure mlaviInitListeP(l: PLAVaisseauxInfos);

    function mlaviAddVaisseaux(l: PLAVaisseauxInfos; gid: integer; quantite: integer): boolean;
    function mlaviModifyVaisseaux(l: PLAVaisseauxInfos; gid: integer; quantite: integer): boolean;

    // renvoi true si ils ont été sélectionné,
    // sinon false si il n'y a pas assez de vaisseaux à quai
    function mlaviSelectionneVaisseaux(l: PLAVaisseauxInfos; Vaisseaux: TCollectionVaisseauSelInfos): boolean;

    function mlaviGetStr(l: PLAVaisseauxInfos): string;




implementation

function mlaviGetStr(l: PLAVaisseauxInfos): string;
var i: integer;
    Buff: string;
begin
   Result := '';

   for i := 0 to CNT_LA_VAISSEAUX_TYPE_MAX_INDICE do begin
      if l[i].gid > 1 then begin
         Buff := format('%d:%d', [l[i].gid, l[i].quantite]);

         if Result = '' then begin
            Result := Buff;
         end else begin
            Result := Result + ', ' + Buff;
         end;

      end;
   end;

end;


function mlaviSelectionneVaisseaux(l: PLAVaisseauxInfos; Vaisseaux: TCollectionVaisseauSelInfos): boolean;
var i: integer;
begin

   Result := false;

   for i := 0 to CNT_LA_VAISSEAUX_TYPE_MAX_INDICE do begin
      if l[i].gid > 1 then begin

         if NoT(Vaisseaux.SetVaisseauxAUtiliser(l[i].gid, l[i].quantite)) then begin
            // pas assez de vaisseaux
            exit;
         end;

      end;
   end;

   // tout a bien été rempli
   Result := true;
end;


function mlaviModifyVaisseaux(l: PLAVaisseauxInfos; gid: integer; quantite: integer): boolean;
var i: integer;
begin

   Result := false;

   for i := 0 to CNT_LA_VAISSEAUX_TYPE_MAX_INDICE do begin
      if (l[i].gid = gid) then begin
         // met à jour la quantité
         l[i].quantite := quantite;
         Result := true;
         break;
      end;

   end;


end;

function mlaviAddVaisseaux(l: PLAVaisseauxInfos; gid: integer; quantite: integer): boolean;
var i: integer;
begin

   if quantite = 0 then begin
      Result := true;
      exit;
   end;

   Result := false;

   for i := 0 to CNT_LA_VAISSEAUX_TYPE_MAX_INDICE do begin
      if (l[i].gid < 1) then begin
         // emplacement libre
         l[i].gid      := gid;
         l[i].quantite := quantite;
         Result := true;
         break;
      end;

   end;

end;


procedure mlaviInitListeP(l: PLAVaisseauxInfos);
var i: integer;
begin

   for i := 0 to CNT_LA_VAISSEAUX_TYPE_MAX_INDICE do begin
      l[i].gid      := -1;
      l[i].quantite := -1;
   end;

end;

procedure mlaviInitListeT(var l: TLAVaisseauxInfos);
begin
   mlaviInitListeP(@l);
end;

end.
